package test1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class test1 {
    //封装两个方法
    public static Connection getConn(){
        Connection conn=null;
        //1.连接数据库之前先加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC","root","123456");//固定写法
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return conn;

    }
    //2.关闭数据库
    public static void clossConn(Connection conn){
        try {
            conn.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {

Connection conn = test1.getConn();
        if (conn != null) {
            System.out.println("连接成功");

        }else {
            System.out.println("连接失败");
        }
        test1.clossConn(conn);
    }
}

